package com.renu.jdependencyanalyser.window.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.renu.jdependencyanalyser.cons.ProjectCons;
import com.renu.jdependencyanalyser.core.ClassBorderGene;
import com.renu.jdependencyanalyser.core.ProjectInfo;
import com.renu.jdependencyanalyser.data.ProjectDataCenter;
import com.renu.jdependencyanalyser.freemarker.DependencyProcessor;
import com.renu.jdependencyanalyser.model.ClassModel;
import com.renu.jdependencyanalyser.utils.MyFileUtil;
import com.renu.jdependencyanalyser.utils.MyProjectUtil;
import com.renu.jdependencyanalyser.visualizer.GraphvizVisualizer;
import com.renu.jdependencyanalyser.window.AnalyserWindow;
import com.renu.jdependencyanalyser.utils.ImageDialogUtil;
import com.renu.jdependencyanalyser.window.PanelShow;

import java.io.File;

public class ClsBorderDependencyAction extends AnAction implements DumbAware {

    ProjectInfo projectInfo = null;

    GraphvizVisualizer graphvizVisualizer = new GraphvizVisualizer();

    ClassBorderGene classBorderGene = null;




    ClassModel  classModel = null;

    @Override
    public void actionPerformed(AnActionEvent e) {
        VirtualFile file = e.getData(CommonDataKeys.VIRTUAL_FILE);
        projectInfo =  ProjectDataCenter.projectInfo;
        if (projectInfo == null){
            System.err.println("project doesn't be initialized");
        }
        if (file!= null){
            String path = file.getPath();
            file:///Users/renu/Documents/high/paper/ProjectTest/src/main/java/org/example/soot
            if (!path.contains(".java")){
                return;
            }
            MyFileUtil.createOutDir(MyProjectUtil.projectBasePath + ProjectCons.INNER_CLASS_INVOKE_DIR);
            String classSig = MyFileUtil.extractClassSigFromJavaPath(path);
            classModel = projectInfo.getClassModel(classSig);
            ProjectDataCenter.classModel = classModel;
            graphvizVisualizer = new GraphvizVisualizer();
            String filePath = MyProjectUtil.projectBasePath +ProjectCons.INNER_CLASS_INVOKE_DIR + File.separator +classSig + ".png";

            graphvizVisualizer.generateGraph(new DependencyProcessor(projectInfo,classModel,ProjectCons.OUT),filePath);

            ImageDialogUtil.showImage(filePath,"查看"+classSig +"关于第三方依赖的依赖图");
            AnalyserWindow analyserWindow = ProjectDataCenter.analyserWindow;

            PanelShow panelShow = new PanelShow(projectInfo);
            panelShow.initCurrentClassPanel(classModel);
        }

    }
    @Override
    public void update(AnActionEvent e) {
        // 通过Condition来限制菜单项显示的条件
        e.getPresentation().setVisible(checkCondition(e));
    }

    private boolean checkCondition(AnActionEvent e) {
        // 检查是否为 Java 文件
        PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
        if (psiFile != null && psiFile.getName().endsWith(".java")) {
            // 如果是 Java 文件，则显示菜单项
            return true;
        }

        // 其他情况不显示菜单项
        return false;
    }
}
